<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Helpers
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class PaginationHelper extends Helper
{
	protected $_link;
	
	protected $count    = 1;
	protected $limit    = 1;
	protected $page     = 1;
	
	protected $numPages;
	protected $prevPage = null;
	protected $nextPage = null;
	
	function set(&$paging)
	{
		$this->page  = $paging['page'];
		$this->count = $paging['count'];
		$this->limit = $paging['limit'];
		$this->_link = $paging['link'];
		
		// no pages?
		if ($this->page == 1 and $this->count < $this->limit)
			return false;
		
		$this->numPages = ceil($this->count / $this->limit);
		$this->nextPage = ($this->page < $this->numPages) ? $this->page + 1 : null;
		$this->prevPage = ($this->page > 1) ? $this->page - 1 : null;
		
		// there are pages
		return true;
	}
	
	function results($text=null, $textone=null)
	{
		if (!empty($this->count))
		{
#			if ($this->count > $this->limit)
#			{
				$start = ($this->page > 1) ? ($this->page - 1) * $this->limit + 1 : 1;
				$end   = ($this->count < $start + $this->limit) ? $this->count : $start + $this->limit - 1;
				return sprintf(isset($text) ? $text : _('Results: %d-%d of %d'), $start, $end, $this->count);
#			}
#			else {
#				return sprintf(isset($textone) ? $textone : _('Results: %d of %d'), $this->count, $this->count);
#			}
		}
	}
	
	function numbers($separator=' ')
	{
		ob_start();
		for ($i=1; $i<=$this->numPages; $i++)
		{
			echo ($i != $this->page) ? $this->link($i, $this->_link.$i) : $i;
			echo $separator;
		}
		return ob_get_clean();
	}
	
	function prev($text=null, $force=false)
	{
		$text = isset($text) ? $text : _('Previous');
		if (!is_null($this->prevPage))
			return $this->link($text, $this->_link.$this->prevPage);
		elseif ($force)
			return $text;
	}
	
	function next($text=null, $force=false)
	{
		$text = isset($text) ? $text : _('Next');
		if (!is_null($this->nextPage))
			return $this->link($text, $this->_link.$this->nextPage);
		elseif ($force)
			return $text;
	}
}
?>